﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace Joyeon.Helpers
{
    public class ProgressHelper
    {
        public delegate void Delegate(Object value);
        public delegate void WorkDelegate(ProgressHelper helper);
        Thread thread = null;
        private Delegate _progress;
        /// <summary>
        /// 设置接收由PostProgress方法发送的进度值信息的方法
        /// </summary>
        /// <param name="progress"></param>
        public ProgressHelper SetProgress(Delegate progress)
        {
            try
            {
                _progress = new Delegate(progress);
            }
            catch { }
            return this;
        }

        private Delegate _max;
        /// <summary>
        /// 设置接收由PostMax方法发送的最大值信息的方法
        /// </summary>
        /// <param name="max"></param>
        public ProgressHelper SetMax(Delegate max)
        {
            try
            {
                _max = new Delegate(max);

            }
            catch { }
            return this;
        }
        private Delegate _success;
        /// <summary>
        /// 设置接收由PostSuccess方法发送对象信息的方法
        /// </summary>
        /// <param name="success"></param>
        public ProgressHelper SetSuccess(Delegate success)
        {
            try
            {
                _success = new Delegate(success);
            }
            catch { }
            return this;
        }
        private Delegate _failed;
        /// <summary>
        /// 设置接收由PostFailed方法发送对象信息的方法
        /// </summary>
        /// <param name="failed"></param>
        public ProgressHelper SetFailed(Delegate failed)
        {
            try
            {
                _failed = new Delegate(failed);

            }
            catch { }
            return this;
        }
        private Delegate _message;
        /// <summary>
        /// 设置接收由PostMessage方法发送的字符串信息的方法
        /// </summary>
        /// <param name="message"></param>
        public ProgressHelper SetMessage(Delegate message)
        {
            try
            {
                _message = new Delegate(message);
            }
            catch { }
            return this;
        }

        private WorkDelegate _work;
        /// <summary>
        /// 设置工作委托
        /// </summary>
        /// <param name="work"></param>
        /// <returns></returns>
        public ProgressHelper SetWork(WorkDelegate work)
        {
            try
            {
                _work = new WorkDelegate(work);

            }
            catch { }
            return this;
        }

        private Dispatcher dispatcher = null;
        public ProgressHelper(FrameworkElement frameworkElement)
        {
            if (frameworkElement == null)
            {
                throw new ArgumentNullException();
            }
            this.dispatcher = frameworkElement.Dispatcher;
        }
        /// <summary>
        /// 这里写你要做的事
        /// </summary>
        private void DoWork()
        {
            if (_work==null)
            {
                throw new Exception("工作函数为空！");
            }
             _work(this);
        }
        /// <summary>
        /// 开始执行DoWork方法中的内容
        /// </summary>
        public ProgressHelper Start()
        {
            thread = new Thread(new ThreadStart(DoWork));
            thread.IsBackground = true;
            thread.Start();
            return this;

        }
        /// <summary>
        /// 发送滚动条的进度值
        /// </summary>
        /// <param name="value"></param>
        public void PostProgress(object value)
        {
            if (_progress!=null)
            {
                this.dispatcher.Invoke(_progress, value);                
            } 
        }
        /// <summary>
        /// 发送滚动条的最大值
        /// </summary>
        /// <param name="max"></param>
        public void PostMax(object max)
        {
            if (_max!=null)
            {
                this.dispatcher.Invoke(_max, max);                
            }
        }
        /// <summary>
        /// 报告成功完成
        /// </summary>
        /// <param name="value"></param>
        public void PostSuccess(Object value)
        {
            if (_success != null) 
            {
                this.dispatcher.Invoke(_success, value);
            }
        }
        /// <summary>
        /// 报告执行失败
        /// </summary>
        /// <param name="value"></param>
        public void PostFailed(Object value)
        {
            if (_failed!=null)
            {
                this.dispatcher.Invoke(_failed, value);                
            }
        }
        /// <summary>
        /// 发送字符串信息
        /// </summary>
        /// <param name="value"></param>
        public void PostMessage(Object value)
        {
            if (_message!=null)
            {
                this.dispatcher.Invoke(_message, value);                
            }
        }

        /// <summary>
        /// 终止线程
        /// </summary>
        public void Abort()
        {
            try
            {
                this.thread.Abort();
            }
            catch (Exception)
            {
            }
        }
    }
}
